package com.example.spring.multi.basic.dingdang;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.spring.multi.basic.dingdang.model.DeliveryDetailsModel;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: knowledge-pooling
 * @ClassName DeliveryDetailsExcelUtil
 * @description: 配送明细EXCEL工具类
 * @author: tomato
 * @create: 2024-07-17 20:19
 * @Version 1.0
 **/
public class DeliveryDetailsExcelUtil {

    //配送明细表：筛选反馈时间为空白和门店收货数量为0然后去掉上一状态为520，980，982，984的数据，等到一张处理后的表
    //原始表筛选有时间和收货数量为0，等到一张处理后的表
    //英克门店ID和英克ID合并
    //然后两张表合并
    //是两次处理后是保留所有列两张表合并，然后英克门店ID和英克ID合并得到新一列，所以总列数比原始表多一列
    public static  Map<String, Integer> dealDeliveryDetailsExcel(File stockFile) {
        List<DeliveryDetailsModel> dataEndList = new ArrayList<>();
        ExcelReader reader = ExcelUtil.getReader(stockFile);
        // 获取总行数
        int totalRows = reader.getRowCount();

        int skipRows = 50000;

        // 计算需要跳过的行数，使得每次读取工作簿的三分之一
        if(totalRows>100000){
            skipRows = (totalRows-1) / 3;
        }
        if(totalRows>150000){
            skipRows = (totalRows-1) / 6;
        }
        // 关闭reader
        reader.close();

        // 循环读取文件，每次跳过三分之一的行数
        for (int i = 1; i < totalRows; i += skipRows) {
            ExcelReader reader1 = ExcelUtil.getReader(stockFile);
            // 读取表头信息,并且将表头信息转换成对应的实体类
            reader1.addHeaderAlias("反馈时间", "feedbackTime");
            reader1.addHeaderAlias("门店收货数量", "storeReceiveCount");
            reader1.addHeaderAlias("上一状态", "lastStatus");
            reader1.addHeaderAlias("英克门店ID", "yekuoStoreId");
            reader1.addHeaderAlias("英克ID", "yekuoId");
            reader1.addHeaderAlias("订单数量", "orderCount");

            // 读取一行数据
            List<DeliveryDetailsModel> dataList = reader1.read(0, i, i + skipRows, DeliveryDetailsModel.class);
            reader1.close();
            // 处理读取到的数据
            dataEndList.addAll(dealDeliveryDetailsExcelData(dataList));
        }
        // 使用Map来存储分组和累加结果
        Map<String, Integer> result = new HashMap<>();
        if (dataEndList.isEmpty()){
            return result;
        }

        // 遍历实体集合
        for (DeliveryDetailsModel entity : dataEndList) {
            String name = entity.getInkDealId();
            int num =  entity.getOrderCount();

            // 如果Map中已经存在这个name，则累加num
            // 否则，将name和num添加到Map中
            result.put(name, result.getOrDefault(name, 0) + num);
        }

        return result;
    }

    private static List<DeliveryDetailsModel> dealDeliveryDetailsExcelData(List<DeliveryDetailsModel> dataList) {
        // 筛选集合A
        List<DeliveryDetailsModel> setA = dataList.stream()
                .filter(e -> ("".equals(e.getFeedbackTime()) || e.getFeedbackTime() == null) && (Objects.isNull(e.getStoreReceiveCount()) || e.getStoreReceiveCount() == 0))
                .filter(e -> !Arrays.asList(520, 980, 982, 984).contains(e.getLastStatus()))
                .peek(DeliveryDetailsModel::calculateInkCode) // 在收集前为每个实体计算英克码
                .collect(Collectors.toList());

        // 筛选集合B
        List<DeliveryDetailsModel> setB = dataList.stream()
                .filter(e -> e.getFeedbackTime() != null && !e.getFeedbackTime().isEmpty() && (Objects.isNull(e.getStoreReceiveCount()) || e.getStoreReceiveCount() == 0))
                .peek(DeliveryDetailsModel::calculateInkCode) // 在收集前为每个实体计算英克码
                .collect(Collectors.toList());

        // 合并集合A和B
        List<DeliveryDetailsModel> setC = new ArrayList<>(setA);
        setC.addAll(setB);
        return setC;

    }
}
